# Comments controller controls user comments/trash talk for each match
# In 13wins, the user may submit comments/trash talk per each scheduled match

class CommentsController < ApplicationController
  # List comments by match_id
  # Create a comment and render a new list (ajax)
  def index
   logger.error " inside comments controller"
   @sid = params[:sid]
   if params[:fb_sig_xid_action] == "post"
     params[:sid] = params[:fb_sig_xid].split("_").last
     m = Match.find(params[:sid].to_i)
     m.comment_count = m.comment_count.to_i + 1
     m.save
     current_user.add_point(PointConfig.find_by_name("user_comment").point)
        logger.error " inside comments controller - post"

     PointHistory.create(:user_id=>current_user.id,:action=>'Bonus for trash talking!',:point=>PointConfig.find_by_name("user_comment").point,:earning => true)
   elsif params[:fb_sig_xid_action] == "delete"
     params[:sid] = params[:fb_sig_xid].split("_").last
     m = Match.find(params[:sid].to_i)
     m.comment_count = m.comment_count.to_i - 1  if m.comment_count.to_i > 1
     m.save
     current_user.add_point(-PointConfig.find_by_name("user_comment").point)
             logger.error " inside comments controller - delete"

   else
     params[:sid] = params[:fb_sig_xid].split("_").last if @sid.nil?
             logger.error " inside comments controller - other"

   end
   render :partial=> "index",:locals=>{:sid=>params[:sid]},:layout=>false
  end

end